1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Windows.Forms;
5
6 namespace WindowsFormsApplication1
7 {
8 public partial class frmPuzzleGame : Form
9 {
10 int inNullSliceIndex, inmoves = 0;
11 List<Bitmap> lstOriginalPictureList = new List<Bitmap>();
12 System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
13
14 public frmPuzzleGame()
15 {
16 InitializeComponent();
17 lstOriginalPictureList.AddRange(new Bitmap[] { Properties.Resources._1, Properties.Resources._2, Properties.Resources._3, Properties.Resources._4, Properties.Resources._5, Properties.Resources._6, Properties.Resources._7, Properties.Resources._8, Properties.Resources._9, Properties.Resources._null });
18 lblMovesMade.Text += inmoves;
19 lblTimeElapsed.Text = "00:00:00";
20 }
21
22 private void Form1_Load(object sender, EventArgs e)
23 {
24 Shuffle();
25 }
26
27 void Shuffle()
28 {
29 do
30 {
31 int j;
32 List<int> Indexes = new List<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 9 });//8 is not present - since it is the last slice.
33 Random r = new Random();
34 for (int i = 0; i < 9; i++)
35 {
36 Indexes.Remove((j = Indexes[r.Next(0, Indexes.Count)]));
37 ((PictureBox)gbPuzzleBox.Controls[i]).Image = lstOriginalPictureList[j];
38 if (j == 9) inNullSliceIndex = i;//store empty picture box index
39 }
40 } while (CheckWin());
41 }
42
43 private void btnShuffle_Click(object sender, EventArgs e)
44 {
45 DialogResult YesOrNo = new DialogResult();
46 if (lblTimeElapsed.Text != "00:00:00")
47 {
48 YesOrNo = MessageBox.Show("Bạn muốn chơi lại ?", "Giải đố hình", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
49 }
50 if (YesOrNo == DialogResult.Yes || lblTimeElapsed.Text == "00:00:00")
51 {
52 Shuffle();
53 timer.Reset();
54 lblTimeElapsed.Text = "00:00:00";
55 inmoves = 0;
56 lblMovesMade.Text = "Lượt Đi : 0";
57 }
58 }
59
60 private void AskPermissionBeforeQuite(object sender, FormClosingEventArgs e)
61 {
62 DialogResult YesOrNO = MessageBox.Show("Bạn có chắc chắn bỏ cuộc ?", "Giải đố hình", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
63 if (sender as Button != btnQuit && YesOrNO == DialogResult.No) e.Cancel = true;
64 if (sender as Button == btnQuit && YesOrNO == DialogResult.Yes) Environment.Exit(0);
65 }
66
67 private void btnQuit_Click(object sender, EventArgs e)
68 {
69 AskPermissionBeforeQuite(sender, e as FormClosingEventArgs);
70 }
71
72 private void SwitchPictureBox(object sender, EventArgs e)
73 {
74 if (lblTimeElapsed.Text == "00:00:00")
75 timer.Start();
76 int inPictureBoxIndex = gbPuzzleBox.Controls.IndexOf(sender as Control);
77 if (inNullSliceIndex != inPictureBoxIndex)
78 {
79 List<int> FourBrothers = new List<int>(new int[] { ((inPictureBoxIndex % 3 == 0) ? -1 : inPictureBoxIndex - 1), inPictureBoxIndex - 3, (inPictureBoxIndex % 3 == 2) ? -1 : inPictureBoxIndex + 1, inPictureBoxIndex + 3 });
80 if (FourBrothers.Contains(inNullSliceIndex))
81 {
82 ((PictureBox)gbPuzzleBox.Controls[inNullSliceIndex]).Image = ((PictureBox)gbPuzzleBox.Controls[inPictureBoxIndex]).Image;
83 ((PictureBox)gbPuzzleBox.Controls[inPictureBoxIndex]).Image = lstOriginalPictureList[9];
84 inNullSliceIndex = inPictureBoxIndex;
85 lblMovesMade.Text = "Lượt Đi : " + (++inmoves);
86 if (CheckWin())
87 {
88 timer.Stop();
89 (gbPuzzleBox.Controls[8] as PictureBox).Image = lstOriginalPictureList[8];
90 MessageBox.Show("Congratulations...\nYour Rabbit Is Happy\nTime Elapsed : " + timer.Elapsed.ToString().Remove(8) + "\nTotal Lượt Đi : " + inmoves, "Rabbit Puzzle");
91 inmoves = 0;
92 lblMovesMade.Text = "Lượt Đi : 0";
93 lblTimeElapsed.Text = "00:00:00";
94 timer.Reset();
95 Shuffle();
96 }
97 }
98 }
99 }
100
101 bool CheckWin()
102 {
103 int i;
104 for (i = 0; i < 8; i++)
105 {
106 if ((gbPuzzleBox.Controls[i] as PictureBox).Image != lstOriginalPictureList[i]) break;
107 }
108 if (i == 8) return true;
109 else return false;
110 }
111
112 private void UpdateTimeElapsed(object sender, EventArgs e)
113 {
114 if (timer.Elapsed.ToString() != "00:00:00")
115 lblTimeElapsed.Text = timer.Elapsed.ToString().Remove(8);
116 if (timer.Elapsed.ToString() == "00:00:00")
117 btnPause.Enabled = false;
118 else
119 btnPause.Enabled = true;
120 if (timer.Elapsed.Minutes.ToString() == "1")
121 {
122 timer.Reset();
123 lblMovesMade.Text = "Lượt Đi : 0";
124 lblTimeElapsed.Text = "00:00:00";
125 inmoves = 0;
126 btnPause.Enabled = false;
127 MessageBox.Show("Hết giờ\nChơi lại", "Giải Đố Hình");
128 Shuffle();
129 }
130 }
131
132 int hinh = 0;
133 private void btnNext_Click(object sender, EventArgs e)
134 {
135 hinh += 1;
136 List<Bitmap> b1 = new List<Bitmap>();
137 lstOriginalPictureList = new List<Bitmap>();
138
139 if (hinh == 1)
140 {
141 b1.Add(Properties.Resources.original1);
142
143 lstOriginalPictureList.AddRange(new Bitmap[] { Properties.Resources._11, Properties.Resources._12, Properties.Resources._13, Properties.Resources._14, Properties.Resources._15, Properties.Resources._16, Properties.Resources._17, Properties.Resources._18, Properties.Resources._19, Properties.Resources._null });
144
145 }
146 else if (hinh == 2)
147 {
148 b1.Add(Properties.Resources.original2);
149
150 lstOriginalPictureList.AddRange(new Bitmap[] { Properties.Resources._21, Properties.Resources._22, Properties.Resources._23, Properties.Resources._24, Properties.Resources._25, Properties.Resources._26, Properties.Resources._27, Properties.Resources._28, Properties.Resources._29, Properties.Resources._null });
151
152 }
153 else if(hinh == 3)
154 {
155 b1.Add(Properties.Resources.original3);
156
157 lstOriginalPictureList.AddRange(new Bitmap[] {
158 Properties.Resources._31, Properties.Resources._32,
159 Properties.Resources._33, Properties.Resources._34,
160 Properties.Resources._35, Properties.Resources._36,
161 Properties.Resources._37, Properties.Resources._38,
162 Properties.Resources._39, Properties.Resources._null });
163
164 }
165 else
166 {
167 b1.Add(Properties.Resources.original4);
168 lstOriginalPictureList.AddRange(new Bitmap[] { Properties.Resources._41,
169 Properties.Resources._42, Properties.Resources._43,
170 Properties.Resources._44, Properties.Resources._45,
171 Properties.Resources._46, Properties.Resources._47,
172 Properties.Resources._48, Properties.Resources._49, Properties.Resources._null });
173
174 hinh = 0;
175 }
176 gbOriginal.BackgroundImage = b1[0];
177 timer.Reset();
178 lblTimeElapsed.Text = "00:00:00";
179 inmoves = 0;
180 lblMovesMade.Text = "Lượt Đi : 0";
181
182 Shuffle();
183
184
185 }
186
187 private void PauseOrResume(object sender, EventArgs e)
188 {
189 if (btnPause.Text == "Tạm Dừng")
190 {
191 timer.Stop();
192 gbPuzzleBox.Visible = false;
193 btnPause.Text = "Tiếp Tục";
194 }
195 else
196 {
197 timer.Start();
198 gbPuzzleBox.Visible = true;
199 btnPause.Text = "Tạm Dừng";
200 }
201 }
202 }
203 }